package wp.ggt.com.wpview.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import org.xutils.x;

import wp.ggt.com.wpview.view.FilterImageView;
import wp.ggt.com.wpview.view.MyViewGroup;

/**
 * Created by KF on 2016/3/18.
 */
public class ViewHolder implements MyViewGroup.ItemClickListener, View.OnTouchListener {
    private View contentView;
    private SparseArray<View> views;
    private Context context;
    private boolean flag = true;

    private ViewHolder(Context context, int LayoutId) {
        contentView = LayoutInflater.from(context).inflate(LayoutId, null);
        views = new SparseArray<>();
        contentView.setTag(this);
        this.context = context;
    }

    public static View getContentView(View contentView, Context context, int LayoutId) {
        if (contentView == null) {
            contentView = new ViewHolder(context, LayoutId).getContentView();
        }
        return contentView;
    }

    public View getContentView() {
        return contentView;
    }

    public <T extends View> T findViewById(int id) {
        View view = views.get(id);
        flag = false;
        if (view == null) {
            flag = true;
            view = contentView.findViewById(id);
            views.put(id, view);
        }
        return (T) view;
    }

    public void setPhoto(String url, int id, int number) {
        MyViewGroup group = findViewById(id);
        group.setItemClickListener(this);
        if (flag)
            for (int i = 0; i < number; i++) {
                FilterImageView imageView = new FilterImageView(context);
                imageView.setOnTouchListener(this);
                imageView.setLayoutParams(new ViewGroup.LayoutParams(500, 500));
                imageView.setScaleType(ImageView.ScaleType.CENTER);
                x.image().bind(imageView, url);
                group.addView(imageView);
            }
    }

    @Override
    public void onItemClickListener(View view, int position) {
        Log.e("TAG", position + "---");
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                setFilter((ImageView) v);
                break;
            default:
                removeFilter((ImageView) v);
                break;
        }
        return false;
    }

    private void setFilter(ImageView v) {
        //先获取设置的src图片
        Drawable drawable = v.getDrawable();
        //当src图片为Null，获取背景图片
        if (drawable == null) {
            drawable = v.getBackground();
        }
        if (drawable != null) {
            //设置滤镜
            drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        }
    }

    private void removeFilter(ImageView v) {
        //先获取设置的src图片
        Drawable drawable = v.getDrawable();
        //当src图片为Null，获取背景图片
        if (drawable == null) {
            drawable = v.getBackground();
        }
        if (drawable != null) {
            //清除滤镜
            drawable.clearColorFilter();
        }
    }
}
